<%@ page import="team.bluepen.supermarket.data.entity.User" %>
<%@ page import="team.bluepen.supermarket.constant.Constant" %>
<%@ page import="team.bluepen.supermarket.constant.Role" %>
<%@ page import="team.bluepen.supermarket.constant.Error" %>
<%@ page import="team.bluepen.supermarket.data.dao.UserRepository" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<%
    User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
    Error error = null;
%>
<%!
    UserRepository repository = new UserRepository();
%>
<head>
<%-- 个人设置页面 --%>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/images/icon.ico" type="/image/x-icon" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
    <script src="${pageContext.request.contextPath}/js/bootstrap.bundle.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/main.css">
    <title>Settings - Supermarket Manage System</title>
</head>
<body>
<main>
    <div class="d-flex flex-column flex-shrink-0 p-3 bg-light" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" style="width: 20%;min-width: 150px">
        <a href="${pageContext.request.contextPath}/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto link-dark text-decoration-none">
            <span class="fs-4">Supermarket Manage System</span>
        </a>
        <hr>
        <ul class="nav nav-pills flex-column mb-auto">
            <li class="nav-item">
                <a href="overview.jsp" class="nav-link link-dark" aria-current="page">
                    Overview
                </a>
            </li>
            <%
                if (user != null && user.getRole() == Role.ADMIN) {
            %>
            <li>
                <a href="account.jsp" class="nav-link link-dark">
                    Accounts
                </a>
            </li>
            <%
                }
            %>
            <li>
                <a href="manage.jsp" class="nav-link link-dark">
                    All Goods
                </a>
            </li>

            <li>
                <a href="query.jsp" class="nav-link link-dark">
                    Goods Query
                </a>
            </li>
            <li>
                <a href="file.jsp" class="nav-link link-dark">
                    File
                </a>
            </li>
        </ul>
        <hr>
        <div class="dropdown">
            <a href="#" class="d-flex align-items-center link-dark text-decoration-none dropdown-toggle" id="dropdownUser2" data-bs-toggle="dropdown" aria-expanded="false">
                <strong><%=user.getUsername()%></strong>
            </a>
            <ul class="dropdown-menu text-small shadow" aria-labelledby="dropdownUser2">
                <li><a class="dropdown-item" href="setting.jsp">Settings</a></li>
                <li><hr class="dropdown-divider"></li>
                <li><form method="post" action="api/logout-control">
                    <button class="dropdown-item" type="submit" value="logout" name="logout">Logout</button>
                </form></li>
            </ul>
        </div>
    </div>
    <%
        String state = request.getParameter("error");
        if (state != null) {
            error = Error.getErrorByText(state);
        }
    %>
    <%   if (error != null && !error.getState()) {%>
    <script>alert('Edit user error! \n<%=error%>' )</script>
    <%
        }
    %>
    <% if (error != null && error.getState()) {
        user = repository.query(user.getRow());
        session.setAttribute(Constant.SESSION_USER, user);
    }
    %>


    <div class="flex-column p-4 flex-grow-1 allow-rolling">
        <h1 class="flex-column">Settings</h1>
        <div class="dropdown-divider"></div>
        <%
            if ( user.getRole() == Role.GUEST) {
        %>
        <h2> Currently in guest mode, unable to operate!</h2>
        <%
            }
        %>
        <h4 class="flex-row">
            <div class="flex-column btn-group-vertical">
                <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#edit_username">Edit Username</button>
                <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#edit_password">Edit Password</button>
            </div>
        </h4>
        <br>
        <h1 class="flex-column">About</h1>
        <hr>
        <p>This project are developed by <strong class="text-primary"><i>Team BluePen</i></strong>.</p>
        <pre class="text-primary"><%=Constant.LOGO_PAINTING%></pre>
    </div>

    <div class="modal fade" id="edit_username">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Edit Username</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form action="api/setting-control" method="post" name="edit_username_form">
                    <div class="modal-body">
                        <input type="text" name="user_row" id="user_row_input1" hidden value="<%=user.getRow()%>">
                        <div class="alert alert-warning">
                            Notice:
                            <br>If you don't want to change the username, please just leave it blank.
                        </div>
                        <input type="text" class="form-control text-input" name="edit_username" id="edit_username_input" placeholder="Type a new username..." >
                    </div>

                    <div class="modal-footer">
                        <div class="btn-group">
                            <button type="submit" class="btn btn-primary" data-bs-dismiss="modal">Confirm</button>
                            <button type="reset" class="btn btn-outline-secondary">Reset</button>
                            <button type="button" class="btn btn-outline-danger" data-bs-dismiss="modal">Cancel</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade" id="edit_password">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Edit Password</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form action="api/setting-control" method="post" name="edit_username_form">
                    <div class="modal-body">
                        <input type="text" name="user_row" id="user_row_input2" hidden value="<%=user.getRow()%>">
                        <div class="alert alert-warning">
                            Notice:
                            <br>If you don't want to change the password, please just leave it blank.
                        </div>
                        <input type="password" class="form-control text-input" name="edit_password" id="edit_password_input" placeholder="Type a new password..." >
                    </div>

                    <div class="modal-footer">
                        <div class="btn-group">
                            <button type="submit" class="btn btn-primary" data-bs-dismiss="modal">Confirm</button>
                            <button type="reset" class="btn btn-outline-secondary">Reset</button>
                            <button type="button" class="btn btn-outline-danger" data-bs-dismiss="modal">Cancel</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

</main>
</body>
</html>
